import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)
// 解决 vue-router 3.0 重复点击相同路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: {},
      },
      {
        path: '/erp',
        name: 'erp',
        component: () => import('@/views/erp/index.vue')
      },
      {
        path: '/selfsign',
        name: 'selfsign',
        component: () => import('@/views/selfsign/index.vue')
      },
      {
        path: '/payloglist',
        name: 'payloglist',
        component: () => import('@/views/payloglist/index.vue')
      },
      {
        path: '/loglist',
        name: 'loglist',
        component: () => import('@/views/loglist/index.vue')
      },
      {
        path: '/customerlog',
        name: 'customerlog',
        component: () => import('@/views/customerlog/index.vue')
      },
      {
        path: '/KDlog',
        name: 'KDlog',
        component: () => import('@/views/KDlog/index.vue')
      },
      {
        path: '/YYlog-BD',
        name: 'YYlog-BD',
        component: () => import('@/views/YYlog-BD/index.vue')
      },
      {
        path: '/YYlog-WL',
        name: 'YYlog-WL',
        component: () => import('@/views/YYlog-WL/index.vue')
      },
      {
        path: '/YYlog-NNJX',
        name: 'YYlog-NNJX',
        component: () => import('@/views/YYlog-NNJX/index.vue')
      },
      {
        path: '/appversion',
        name: 'appversion',
        component: () => import('@/views/appversion/index.vue')
      },
      {
        path: '/warehouse',
        name: 'warehouse',
        component: () => import('@/views/warehouse/index.vue')
      },
      {
        path: '/KDcompare',
        name: 'KDcompare',
        component: () => import('@/views/KDcompare/index.vue')
      },
      {
        path: '/YYcompare',
        name: 'YYcompare',
        component: () => import('@/views/YYcompare/index.vue')
      },
      {
        path: '/GYcompare',
        name: 'GYcompare',
        component: () => import('@/views/GYcompare/index.vue')
      },
      {
        path: '/SCScompare',
        name: 'SCScompare',
        component: () => import('@/views/SCScompare/index.vue')
      },
      {
        path: '/GYlist',
        name: 'GYlist',
        component: () => import('@/views/GYlist/index.vue')
      },
      {
        path: '/GYlog',
        name: 'GYlog',
        component: () => import('@/views/GYlog/index.vue')
      },
      {
        path: '/XBBGoodsCompare',
        name: 'XBBGoodsCompare',
        component: () => import('@/views/XBBcompare/goods.vue')
      },
      {
        path: '/XBBCustomCompare',
        name: 'XBBCustomCompare',
        component: () => import('@/views/XBBcompare/custom.vue')
      },
      {
        path: '/XBBUnitCompare',
        name: 'XBBUnitCompare',
        component: () => import('@/views/XBBcompare/unit.vue')
      },
      {
        path: '/MTRefund',
        name: 'MTRefund',
        component: () => import('@/views/MTRefund/index.vue')
      },
      {
        path: '/SFcompare',
        name: 'SFcompare',
        component: () => import('@/views/SFcompare/index.vue')
      },
      {
        path: '/SFlog',
        name: 'SFlog',
        component: () => import('@/views/SFlog/index.vue')
      },
      {
        path: '/XBBlog',
        name: 'XBBlog',
        component: () => import('@/views/XBBlog/index.vue')
      },
      {
        path: '/FXSKDlog',
        name: 'FXSKDlog',
        component: () => import('@/views/FXSKDlog/index.vue')
      },
      {
        path: '/WDTcompare',
        name: 'WDTcompare',
        component: () => import('@/views/WDTcompare/index.vue')
      },
      {
        path: '/AgentList',
        name: 'AgentList',
        component: () => import('@/views/Agent/index.vue')
      },
      {
        path: '/withdraw',
        name: 'withdraw',
        component: () => import('@/views/withdraw/index.vue')
      },
      {
        path: '/money',
        name: 'money',
        component: () => import('@/views/Agent/money.vue')
      },
    ],
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  // 如果不存在token,证明未登录,跳转到登录页
  console.log('进入路由拦截')
  console.log(localStorage.getItem('token'))
  if (!localStorage.getItem('token')) {
    console.log(`to`)
    console.log(to)
    console.log(`from`)
    console.log(from)
    console.log('from')
    console.log(next)
    let pathAry = ['/login']
    if (!pathAry.includes(to.path)) {
      return next('/login')
    }
  }
  console.log('状态管理state')
  console.log(store.state);
  next()
})
// router.beforeEach((to, from, next) => {

  // if (document.cookie.split('=')[1]) { // 判断当前的token是否存在 ； 登录存入的token
  //   next();
  // }
  // else {
  //   next({
  //     path: '/login',
  //     query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
  //   })
  // }
// })
export default router
